import { Texture, TextureFormat, tt } from "../common.js";

export class GameResource {
    static GetRootPath() {
        return "gamedata/";
    }
    static mapTex: { [id: string]: Texture } = {}
    static mapMap: { [id: string]: GameMap } = {}

    static async LoadTexture(name: string): Promise<Texture> {
        let tex = this.mapTex[name];
        if (tex != undefined)
            return tex;
        let img = await tt.loaderex.LoadImageAsync(this.GetRootPath() + name);
        tex = new Texture(tt.graphic.GetWebGL(), img.width, img.height, TextureFormat.RGBA32, null);
        tex.UploadImg(img);
        this.mapTex[name] = tex;
        return tex;

    }
    static async LoadMap(name: string): Promise<GameMap> {
        let map = this.mapMap[name];
        if (map != undefined)
            return map;
        let csv = await tt.loader.LoadStringAsync(this.GetRootPath() + name);

        map = new GameMap();
        map.LoadFromCsv(csv);
        map.CollLines();
        this.mapMap[name] = map;
        return map;
    }
}
export class GameCellLine {
    posA: GameGridPos;
    posB: GameGridPos;
    atob: boolean;
    btoa: boolean;
}
export class GameMap {
    width: number;
    height: number;
    grids: { [id: string]: GameMapCell } = {};
    gridPos2ID: { [id: string]: string } = {}
    lines: { [id: string]: GameCellLine } = {}
    LoadFromCsv(csv: string) {
        let lines = csv.split('\n');
        let splitLines: string[][] = [];
        for (var i = 0; i < lines.length; i++) {
            let line = lines[i];
            if (line == "") continue;
            var words = line.split(",");
            splitLines.push(words);
        }
        let gridx = (splitLines[0].length / 3) | 0;
        let gridy = (splitLines.length / 3) | 0;
        this.width = gridx;
        this.height = gridy;
        for (var x = 0; x < gridx; x++) {
            for (var y = 0; y < gridy; y++) {
                let cword = splitLines[y * 3 + 1][x * 3 + 1];
                if (cword.length > 2) {
                    let cell = new GameMapCell();
                    let cwords = cword.split(":");
                    cell.id = cwords[0];
                    cell.title = cwords[1];
                    cell.pos = { gridx: x, gridy: y }
                    for (var i = 0; i < 3; i++) {
                        for (var j = 0; j < 3; j++) {
                            if (i == 1 && j == 1) continue;
                            let bword = splitLines[y * 3 + j][x * 3 + i];
                            if (bword == "1") {
                                cell.linkto.push({ gridx: x - 1 + i, gridy: y - 1 + j });
                            }
                        }
                    }
                    this.grids[cell.id] = cell;
                    this.gridPos2ID[x + "," + y] = cell.id
                }

            }
        }

    }
    CollLines() {
        this.lines = {};
        for (var id in this.grids) {
            let cell = this.grids[id];
            for (var i = 0; i < cell.linkto.length; i++) {
                let linkto = cell.linkto[i];
                let a = linkto;
                let b = cell.pos;
                let sortvalue = a.gridy * 10000 + a.gridx;
                let sortvalueb = b.gridy * 10000 + b.gridx;
                let btoa = true;
                let atob = false;
                if (sortvalue > sortvalueb) {
                    //交换
                    let temp = a;
                    a = b;
                    b = temp;
                    atob = true;
                    btoa = false;
                }
                let key = a.gridx + "," + a.gridy + "-" + b.gridx + "," + b.gridy;
                let line: GameCellLine = this.lines[key];
                if (line == undefined) {
                    line = this.lines[key] = new GameCellLine();
                    line.posA = a;
                    line.posB = b;
                    this.lines[key] = line;
                }

                if (atob)
                    line.atob = true;
                if (btoa)
                    line.btoa = true;



            }
        }
    }

}
export class GameGridPos {
    gridx: number;
    gridy: number;
}
export class GameMapCell {
    id: string;
    title: string;
    pos: GameGridPos;
    linkto: GameGridPos[] = []
}